package cn.sxt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sxt.sys.constast.SYS_Constast;
import cn.sxt.sys.domain.Permission;
import cn.sxt.sys.domain.Role;
import cn.sxt.sys.service.PermissionService;
import cn.sxt.sys.service.RoleService;
import cn.sxt.sys.utils.DataGridView;
import cn.sxt.sys.utils.TreeNode;
import cn.sxt.sys.vo.PermissionVo;
import cn.sxt.sys.vo.RoleVo;

@Controller
@RequestMapping("role")
public class RoleController {

	@Autowired
	private RoleService roleService;

	@Autowired
	private PermissionService permissionService;

	// 跳到角色管理roleManager.jsp
	@RequestMapping("toRoleManager")
	public String toRoleManager() {
		return "system/role/roleManager";
	}

	// 登陆角色全查询，分页，模糊
	@RequestMapping("queryAllRole")
	@ResponseBody
	public DataGridView queryAllRole(RoleVo roleVo) {
		return roleService.queryAllRole(roleVo);
	}

	// 跳添加页面
	@RequestMapping("toAddRole")
	public String toAddRole() {
		return "system/role/roleAdd";
	}

	// 添加
	@RequestMapping("addRole")
	@ResponseBody
	public Map<String, Object> addRole(RoleVo roleVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = roleService.addRole(roleVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdateRole")
	public String toUpdateRole(RoleVo roleVo, Model model) {
		Role role = roleService.queryRoleById(roleVo.getId());
		model.addAttribute("role", role);
		return "system/role/roleUpdate";
	}

	// 修改
	@RequestMapping("updateRole")
	@ResponseBody
	public Map<String, Object> updateRole(RoleVo roleVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = roleService.updateRole(roleVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳分配角色页面
	@RequestMapping("toSelectPermission")
	public String toSelectPermission(RoleVo roleVo) {
		return "system/role/selectPermission";
	}

	// 根据id加载角色权限 复选zTree
	@RequestMapping("loadRolePermissionTree")
	@ResponseBody
	public List<TreeNode> loadRolePermissionTree(RoleVo roleVo) {
		PermissionVo permissionVo = new PermissionVo();
		permissionVo.setAvailable(SYS_Constast.TYPE_AVAILABLE_YES);
		Integer roleid = roleVo.getId();
		// 根据角色id查已有权限
		List<Permission> curRolePermission = permissionService.queryPermissionByRoleIdForList(permissionVo, roleid);

		// 查所有可用的 权限、菜单
		List<Permission> allPermission = permissionService.queryPermissionForList(permissionVo);
		List<TreeNode> treeNodes = new ArrayList<>();

		for (Permission p1 : allPermission) {
			Boolean checked = false;
			for (Permission p2 : curRolePermission) {
				if (p1.getId() == p2.getId()) {
					checked = true;
					break;
				}
			}
			Boolean open = p1.getOpen() == SYS_Constast.TYPE_PUBLIC_ONE ? true : false;
			Boolean isParent = false;
			if (p1.getType().equals(SYS_Constast.PERMISSION_TYPE_MENU)) {
				isParent = true;
			}
			treeNodes.add(new TreeNode(p1.getId(), p1.getPid(), checked, p1.getName(), open, isParent));
		}
		return treeNodes;
	}

	// 保存角色和权限菜单之间的关系
	@RequestMapping("saveRolePermission")
	@ResponseBody
	public Map<String, Object> saveRolePermission(RoleVo roleVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = roleService.saveRolePermission(roleVo);
			if (i > 0) {
				msg = "分配成功";
			} else {
				msg = "分配失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "分配失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除角色
	@RequestMapping("deleteRole")
	@ResponseBody
	public Map<String, Object> deleteRole(RoleVo roleVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = roleService.deleteRoleById(roleVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 批量删除角色
	@RequestMapping("batchDeleteRole")
	@ResponseBody
	public Map<String, Object> batchDeleteRole(RoleVo roleVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			Integer[] ids = roleVo.getIds();
			if (ids != null && ids.length > 0) {
				for (int i = 0; i < ids.length; i++) {
					roleVo.setId(ids[i]);
					int j = roleService.deleteRoleById(roleVo);
					if (j > 0) {
						msg = "删除成功";
					} else {
						msg = "删除失败";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}
}
